'use strict'

require('./index.css');
require('page/common/nav-simple/index.js');
var _user = require('service/user-service.js');
var formError = {
    show : function(msg){
        $('.error-item').show().find('.error-msg').text(msg);
    },
    hide : function(){
        $('.error-item').hide().find('.error-msg').text('');
    }
};
var page = {
    data : {
        username : '',
        question : '',
        answer : '',
        token : '',
    },
    init : function(){
        this.onload();
        this.bindEvent();
    },
    onload : function(){
        this.loadStepUsername();
    },
    bindEvent : function(){
        var _this = this;
        //用户名页下一步点击事件
        $('#submit-username').click(function(){
            var username = $.trim($('#username').val());
            if(username){
//                _user.getQuestion(username,function(res){
//                    _this.data.username = username;
//                    _this.data.question = res;
//                    _this.loadStepQuestion();
//                },function(errMsg){
//                    formError.show(errMsg);
//                });
                    _this.data.username = username;
                    _this.data.question = 'wo';
                    _this.loadStepQuestion();
            }else{
                formError.show('用户名不能为空');
            }
        });
        //密码提示问题页下一步点击事件
        $('#submit-question').click(function(){
            var answer = $.trim($('#answer').val());
            console.log('answer=='+answer);
            if(answer){
                _user.checkAnswer({
                    username : _this.data.username,
                    question : _this.data.question,
                    answer : answer
                },function(res){
                    _this.data.answer = answer;
                    _this.data.token = res;
                    _this.loadNewPassword();
                },function(errMsg){
                    formError.show(errMsg);
                });
            }else{
                formError.show('密码提示问题答案不能为空');
            }
        });
        //提交新密码页事件
        $('#submit-password').click(function(){
            var password = $.trim($('#password').val());
            if(password){
                _user.resetPassword({
                    username : _this.data.username,
                    passwordNew : password,
                    forgetToken : _this.data.token
                },function(res){
                    console.log(res);
//                    window.location.href = './result.html?type=pass-reset';
                },function(errMsg){
                    console.log('errMsg=='+errMsg);
                    formError.show(errMsg);
                });
            }else{
                formError.show('新密码不能为空');
            }
        });
    },
    //显示输入用户名
    loadStepUsername : function(){
        $('.step-username').show();
    },
    //显示输入密码提示问题答案
    loadStepQuestion : function(){
        formError.hide();
        $('.step-username').hide().siblings('.step-question').show()
            .find('.question').text(this.data.question);
    },
    //显示输入新密码
    loadNewPassword : function(){
        formError.hide();
        $('.step-question').hide().siblings('.step-password').show();
    }
};
$(function(){
    page.init();
});